import { Col, Row } from "antd";
import dayjs from "dayjs";
import { useEffect, useReducer } from "react";
import client from '../../utils/client';

function parser(k, v) {
  // 识别ISO8601格式UTC时间，转换为dayjs类型
  if (typeof v === 'string' && v.length >= 19 && v.length <= 24 && /^(\d{4})-(\d{2})-(\d{2})/.test(v)) {
    const d = dayjs(v);
    return d.isValid() ? d : v;
  }
  return v;
}

export default function HealthInfo() {
  const [state, dispatch] = useReducer((state, info) => {
    if (state.Now) {
      const t_time = info.Now - state.Now;
      const p_time = dayjs(`1970-01-01T${info.TotalProcessorTime}Z`).valueOf() - dayjs(`1970-01-01T${state.TotalProcessorTime}Z`).valueOf();
      info.ProcessorUsage = (p_time / t_time * 100).toFixed(2);
    }
    return info;
  }, {});

  useEffect(() => {
    const reload = () => {
      client._job('Diagnostics', {}, parser).then(info => dispatch(info));
    };
    const t = setInterval(reload, 3000);
    reload();
    return () => clearInterval(t);
  }, []);

  if (state.Now == null) return;

  return (
    <div style={{ width: 480, padding: 10, lineHeight: '30px' }}>
      <Row>
        <Col span={6}>服务器名称: </Col>
        <Col span={18}>{state.MachineName}</Col>
      </Row>
      <Row>
        <Col span={6}>服务器系统: </Col>
        <Col span={18}>{state.OSVersion.VersionString}</Col>
      </Row>
      <Row>
        <Col span={6}>.NET版本: </Col>
        <Col span={18}>{state.CLRVersion}</Col>
      </Row>
      <Row>
        <Col span={6}>平台版本: </Col>
        <Col span={18}>{state.Version}</Col>
      </Row>
      <Row>
        <Col span={6}>当前时间: </Col>
        <Col span={18}>{state.Now.format('YYYY-MM-DD HH:mm:ss')} ({state.Now - dayjs()}ms)</Col>
      </Row>
      <Row>
        <Col span={6}>64位模式: </Col>
        <Col span={18}>{state.Is64BitProcess ? '是' : '否'}</Col>
      </Row>
      <Row>
        <Col span={6}>逻辑CPU: </Col>
        <Col span={18}>{state.ProcessorCount}</Col>
      </Row>
      <Row>
        <Col span={6}>线程数量: </Col>
        <Col span={18}>{state.ThreadCount}</Col>
      </Row>
      <Row>
        <Col span={6}>句柄数量: </Col>
        <Col span={18}>{state.HandleCount}</Col>
      </Row>
      <Row>
        <Col span={6}>引擎总数量: </Col>
        <Col span={18}>{state.TotalEngineCount}</Col>
      </Row>
      <Row>
        <Col span={6}>空闲引擎数量: </Col>
        <Col span={18}>{state.FreeEngineCount}</Col>
      </Row>
      <hr />
      <Row>
        <Col span={6}>启动时间: </Col>
        <Col span={18}>{state.StartTime.format('YYYY-MM-DD HH:mm:ss')}</Col>
      </Row>
      <Row>
        <Col span={6}>持续时间: </Col>
        <Col span={18}>{state.Now.diff(state.StartTime, 'day')}天 {dayjs(state.Now - state.StartTime).format('HH小时 mm分钟 ss秒')}</Col>
      </Row>
      <Row>
        <Col span={6}>CPU使用率: </Col>
        <Col span={18}>{state.ProcessorUsage || '-'} %</Col>
      </Row>
      <Row>
        <Col span={6}>物理内存: </Col>
        <Col span={18}>{Math.round(state.WorkingSet64 / 1024 / 1024)} MB</Col>
      </Row>
      <Row>
        <Col span={6}>最大物理内存: </Col>
        <Col span={18}>{Math.round(state.PeakWorkingSet64 / 1024 / 1024)} MB</Col>
      </Row>
      <Row>
        <Col span={6}>专用内存: </Col>
        <Col span={18}>{Math.round(state.PrivateMemorySize64 / 1024 / 1024)} MB</Col>
      </Row>
    </div>
  );
}